package com.classroompie.classroompieserver.service.impl;

import com.classroompie.classroompiecommon.context.BaseContext;
import com.classroompie.classroompiepojo.entity.*;
import com.classroompie.classroompiepojo.request.AddHomeWorkInput;
import com.classroompie.classroompiepojo.request.SubmitHomeworkInput;
import com.classroompie.classroompiepojo.response.*;
import com.classroompie.classroompieserver.mapper.*;
import com.classroompie.classroompieserver.service.HomeworkService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

import static com.classroompie.classroompiepojo.entity.table.HomeworkModelTableDef.HOMEWORK_MODEL;
import static com.classroompie.classroompiepojo.entity.table.StudentHomeworkModelTableDef.STUDENT_HOMEWORK_MODEL;
import static com.classroompie.classroompiepojo.entity.table.UserCourseModelTableDef.USER_COURSE_MODEL;

/**
 * @author jiahao
 * @since 2024/6/22 周六
 */
@Service
public class HomeworkServiceImpl implements HomeworkService {
    private final HomeworkMapper homeworkMapper;
    private final HomeworkStudentMapper homeworkStudentMapper;
    private final UserCourseMapper userCourseMapper;
    private final UserMapper userMapper;

    public HomeworkServiceImpl(HomeworkMapper homeworkMapper, HomeworkStudentMapper homeworkStudentMapper, UserCourseMapper userCourseMapper, UserMapper userMapper) {
        this.homeworkMapper = homeworkMapper;
        this.homeworkStudentMapper = homeworkStudentMapper;
        this.userCourseMapper = userCourseMapper;
        this.userMapper = userMapper;
    }


    @Override
    public void addHomework(AddHomeWorkInput addHomeWorkInput) {
        HomeworkModel homeworkModel = new HomeworkModel();
        BeanUtils.copyProperties(addHomeWorkInput, homeworkModel);
        homeworkModel.setReleaseTime(LocalDateTime.ofInstant(Instant.ofEpochMilli(addHomeWorkInput.getReleaseTime()), ZoneId.systemDefault()));
        homeworkModel.setDeadline(LocalDateTime.ofInstant(Instant.ofEpochMilli(addHomeWorkInput.getDeadline()), ZoneId.systemDefault()));
        homeworkModel.setCourseId(addHomeWorkInput.getCourseId());
        homeworkModel.setCreateUser(BaseContext.getCurrentId());
        homeworkMapper.insertSelective(homeworkModel);
    }

    @Override
    public List<GetTeacherHomeworkVO> getTeacherHomework(String courseId) {
        List<GetTeacherHomeworkVO> getTeacherHomeworkVOS = new ArrayList<>();
        int allStudents = userCourseMapper.selectListByCondition(USER_COURSE_MODEL.COURSE_ID.eq(courseId)).size();
        List<HomeworkModel> homeworkModels = homeworkMapper.selectListByCondition(HOMEWORK_MODEL.COURSE_ID.eq(courseId));
        for (HomeworkModel homeworkModel : homeworkModels) {
            GetTeacherHomeworkVO getTeacherHomeworkVO = new GetTeacherHomeworkVO();
            BeanUtils.copyProperties(homeworkModel, getTeacherHomeworkVO);
            String homeworkModelId = homeworkModel.getId();
            getTeacherHomeworkVO.setHomeworkId(homeworkModelId);
            List<StudentHomeworkModel> studentHomeworkModels = homeworkStudentMapper.selectListByCondition(STUDENT_HOMEWORK_MODEL.HID.eq(homeworkModelId));
            // 未提交人数
            int submitCounts = studentHomeworkModels.size();
            int noSubmitCounts = allStudents - submitCounts;
            getTeacherHomeworkVO.setNoSubmitCounts((long) noSubmitCounts);
            // 未批改人数
            long noCorrectCounts = studentHomeworkModels.stream().filter(studentHomeworkModel -> studentHomeworkModel.getScore() == null).count();
            getTeacherHomeworkVO.setNoCorrectCounts(noCorrectCounts);
            // 已批改人数
            long correctCounts = submitCounts - noCorrectCounts;
            getTeacherHomeworkVO.setCorrectCounts(correctCounts);
            getTeacherHomeworkVOS.add(getTeacherHomeworkVO);
        }
        return getTeacherHomeworkVOS;
    }


    @Override
    public List<CorrectHomeworkVO> getCorrectHomeworkList(String homeworkId) {
        List<StudentHomeworkModel> studentHomeworkModels = homeworkStudentMapper.selectListByCondition(STUDENT_HOMEWORK_MODEL.HID.eq(homeworkId));
        List<CorrectHomeworkVO> correctHomeworkVOS = new ArrayList<>();
        for (StudentHomeworkModel studentHomeworkModel : studentHomeworkModels) {
            CorrectHomeworkVO correctHomeworkVO = new CorrectHomeworkVO();
            UserModel userModel = userMapper.selectOneById(studentHomeworkModel.getSid());
            correctHomeworkVO.setStudentNo(userModel.getSno());
            correctHomeworkVO.setStudentId(userModel.getId());
            correctHomeworkVO.setStudentName(userModel.getName());
            correctHomeworkVO.setCreateAt(studentHomeworkModel.getCreateAt());
            correctHomeworkVO.setScore(studentHomeworkModel.getScore());
            correctHomeworkVOS.add(correctHomeworkVO);
        }
        return correctHomeworkVOS;
    }

    @Override
    public TeacherHomeWorkDetailVO getHomeworkDetail(String homeworkId) {
        HomeworkModel homeworkModel = homeworkMapper.selectOneById(homeworkId);
        TeacherHomeWorkDetailVO teacherHomeWorkDetailVO = new TeacherHomeWorkDetailVO();
        BeanUtils.copyProperties(homeworkModel, teacherHomeWorkDetailVO);
        List<UserCourseModel> userCourseModels = userCourseMapper.selectListByCondition(USER_COURSE_MODEL.COURSE_ID.eq(homeworkModel.getCourseId()));
        long allStudentCount = userCourseModels.size();
        teacherHomeWorkDetailVO.setStudentCount(allStudentCount);
        List<StudentHomeworkModel> studentHomeworkModels = homeworkStudentMapper.selectListByCondition(STUDENT_HOMEWORK_MODEL.HID.eq(homeworkId));
        long submitCounts = studentHomeworkModels.size();
        // 未提交人数
        long noSubmitCounts =  allStudentCount - submitCounts;
        teacherHomeWorkDetailVO.setNoSubmitCount(noSubmitCounts);
        // 提交人数
        teacherHomeWorkDetailVO.setSubmitCount(submitCounts);
        // 未批改人数
        long noCorrectCounts = studentHomeworkModels.stream().filter(studentHomeworkModel -> studentHomeworkModel.getScore() == null).count();
        teacherHomeWorkDetailVO.setNoCorrectCount(noCorrectCounts);
        // 已批改人数
        long correctCounts = submitCounts - noCorrectCounts;
        teacherHomeWorkDetailVO.setCorrectCount(correctCounts);
        return teacherHomeWorkDetailVO;
    }

    @Override
    public List<GetStudentHomeworkVO> getStudentHomework(String courseId) {
        List<HomeworkModel> homeworkModels = homeworkMapper.selectListByCondition(HOMEWORK_MODEL.COURSE_ID.eq(courseId));
        List<GetStudentHomeworkVO> getStudentHomeworkVOS = new ArrayList<>();
        for (HomeworkModel homeworkModel : homeworkModels) {
            GetStudentHomeworkVO getStudentHomeworkVO = new GetStudentHomeworkVO();
            BeanUtils.copyProperties(homeworkModel, getStudentHomeworkVO);
            String homeworkModelId = homeworkModel.getId();
            getStudentHomeworkVO.setHomeworkId(homeworkModelId);
            StudentHomeworkModel studentHomeworkModel = homeworkStudentMapper.selectOneByCondition(STUDENT_HOMEWORK_MODEL.HID.eq(homeworkModelId).and(STUDENT_HOMEWORK_MODEL.SID.eq(BaseContext.getCurrentId())));
            if (studentHomeworkModel != null) {
                getStudentHomeworkVO.setIsSubmit(true);
                getStudentHomeworkVO.setScore(studentHomeworkModel.getScore());
            } else {
                getStudentHomeworkVO.setIsSubmit(false);
            }
            getStudentHomeworkVOS.add(getStudentHomeworkVO);
        }
        return getStudentHomeworkVOS;
    }

    @Override
    public StudentHomeworkDetailVO getStudentHomeworkDetail(String homeworkId) {
        HomeworkModel homeworkModel = homeworkMapper.selectOneById(homeworkId);
        StudentHomeworkDetailVO studentHomeworkDetailVO = new StudentHomeworkDetailVO();
        BeanUtils.copyProperties(homeworkModel, studentHomeworkDetailVO);
        String currentId = BaseContext.getCurrentId();
        StudentHomeworkModel studentHomeworkModel = homeworkStudentMapper.selectOneByCondition(STUDENT_HOMEWORK_MODEL.HID.eq(homeworkId).and(STUDENT_HOMEWORK_MODEL.SID.eq(currentId)));
        if (studentHomeworkModel != null) {
            studentHomeworkDetailVO.setIsSubmit(true);
            studentHomeworkDetailVO.setSubmitFile(studentHomeworkModel.getFile());
            studentHomeworkDetailVO.setGotScore(studentHomeworkModel.getScore());
        } else {
            studentHomeworkDetailVO.setIsSubmit(false);
        }
        return studentHomeworkDetailVO;
    }

    @Override
    public void submitHomework(String homeworkId, SubmitHomeworkInput submitHomeworkInput) {
        StudentHomeworkModel studentHomeworkModel = new StudentHomeworkModel();
        studentHomeworkModel.setHid(homeworkId);
        studentHomeworkModel.setSid(BaseContext.getCurrentId());
        studentHomeworkModel.setFile(submitHomeworkInput.getFile());
        studentHomeworkModel.setCreateAt(LocalDateTime.now());
        homeworkStudentMapper.insertSelective(studentHomeworkModel);
    }

    @Override
    public void correctHomework(String homeworkId, String studentId,Double score) {
        StudentHomeworkModel studentHomeworkModel = new StudentHomeworkModel();
        studentHomeworkModel.setScore(score);
        homeworkStudentMapper.updateByCondition(studentHomeworkModel, STUDENT_HOMEWORK_MODEL.HID.eq(homeworkId).and(STUDENT_HOMEWORK_MODEL.SID.eq(studentId)));
    }
}
